﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using System.Windows.Forms;
using NPOI.SS.UserModel;
using NPOI.HSSF.UserModel;
using NPOI.XSSF.UserModel;
using System.Data;
using Common;

namespace PaySlips {
    public partial class Import : System.Web.UI.Page {

        protected void Page_Load(object sender, EventArgs e) {
            if (!IsPostBack) {
                //Response.Write("hello");
            }
            else {
                returnMsg.Text = "";
                lblMsg.Text = "";
            }
        }

        protected void btnSubmit_Click(object sender, EventArgs e) {
            if (!FileUpload1.HasFile) {
                lblMsg.Text = "请选择需要上传的文件。";
                return;
            }
            string extFileName = Path.GetExtension(FileUpload1.FileName);
            string fileName = Server.MapPath("~/uploadfiles/") + DateTime.Now.ToFileTime().ToString() + extFileName;
            if (extFileName == ".xls" || extFileName == ".xlsx") {
                FileUpload1.SaveAs(fileName);
                returnMsg.Text = "上传文件成功。" + fileName;
                DataTable dt = Excel.ImportExcel(fileName);
                DataRow dr;
                for (int i = 0; i < dt.Rows.Count; i++) {
                    dr = dt.Rows[i];
                    Response.Write(dr[0].ToString() + dr[1].ToString() + dr[2].ToString() + dr[3].ToString() + dr[4].ToString());
                    Response.Write("<br />");
                }
            }
            else {
                returnMsg.Text = "上传文件格式不对：" + FileUpload1.FileName ;
                return;
            }
        }
    }
}